package group.raber.jeki.controller

import group.raber.auth.entity.User
import group.raber.jeki.entity.Domain
import group.raber.saber.jdbc.accessor.DataAccessor
import group.raber.saber.kit.MapKit
import group.raber.saber.kit.NumberKit
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.servlet.ModelAndView

@Controller
@RequestMapping("/admin")
class AdminController {
    @Autowired
    DataAccessor dataAccessor;

    @RequestMapping("/home")
    ModelAndView home() {
        return new ModelAndView("views/admin/home");
    }
    @RequestMapping("/article-list")
    ModelAndView articleList() {
        Map<String,Object> vars = new HashMap<String,Object>();

        return new ModelAndView("views/admin/article-list",vars);
    }
    @RequestMapping("/profile")
    ModelAndView profile() {
        Map<String,Object> vars = new HashMap<String,Object>();
        return new ModelAndView("views/admin/profile",vars);
    }
    @RequestMapping("/user-list")
    ModelAndView userList() {
        List<User> userList = dataAccessor.selectList(User.class,"select * from AUTH_USER where 1=1 order by ID asc",[:])
        Map<String,Object> vars = new HashMap<String,Object>();
        return new ModelAndView("views/admin/user-list",["userList":userList]);
    }


    @RequestMapping("/domain-tag")
    ModelAndView domainTag() {
        Map<String,Object> vars = new HashMap<String,Object>();
        return new ModelAndView("views/admin/domain-tag",vars);
    }
    @RequestMapping("/domain-create")
    ModelAndView domainCreate() {
        List<Domain> domainList = null;
        return new ModelAndView("views/admin/domain-editor",["domainList":domainList]);
    }
    @RequestMapping(path="/domain-create-action",method = RequestMethod.POST)
    ModelAndView domainCreateAction(@ModelAttribute Domain dataDomain) {
        dataDomain.setId(NumberKit.nanoTime36());
        dataAccessor.insert(dataDomain);
        return new ModelAndView("redirect:/admin/domain-tag");
    }



}
